<template>
  <div class="page_box">
    <!-- 搜索区域 开始 -->
    <el-card shadow="never">
      <el-form :inline="true" size="small" :model="formInline" class="demo-form-inline">
        <el-form-item label="渠道名称：">
          <el-input v-model="formInline.channelName" placeholder="渠道名称"></el-input>
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker v-model="orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="default" icon="el-icon-refresh" @click="rest">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 搜索区域 end -->

    <!-- 操作区域 开始 -->
    <div class="action_box">
      <el-button type="primary" size="small" plain icon="el-icon-plus" @click="addEvent()">新增产品</el-button>
      <el-button type="primary" size="small" plain icon="el-icon-plus" :loading="eloading" @click="exportExcel()">导出excel</el-button>
    </div>
    <!-- 操作区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <el-table class="tab" size="small" :data="tableData" border stripe>
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" show-overflow-tooltip />
        <el-table-column prop="periods" label="产品期数" show-overflow-tooltip />
        <el-table-column prop="dayInterestRate" label="产品利率" show-overflow-tooltip />
        <el-table-column prop="maxAmount" label="最高额度" show-overflow-tooltip />
        <el-table-column prop="type" label="产品级别" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.type == 1 ? "VIP会员" : "普通会员"
          }}</template>
        </el-table-column>
        <el-table-column prop="uv" label="产品UV" show-overflow-tooltip />
        <el-table-column prop="clickCnt" label="点击数" show-overflow-tooltip />
        <el-table-column prop="enableStatus" label="产品状态" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.enableStatus == 1 ? "上架" : "下架"
          }}</template>
        </el-table-column>
        <el-table-column prop="orderNum" label="排序号" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" align="center" width="280">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" plain @click="update(scope.row)">{{ scope.row.enableStatus == 1 ? "下架" : "上架" }}</el-button>
            <el-button type="success" size="mini" plain @click="openConfig(scope.row)">自定义配置</el-button>
            <el-button type="primary" size="mini" plain @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 弹框 开始 -->
    <glsx-dialog :show="showDialog" title="自定义配置" @close="closeConfig">
      <div>
        <el-form :model="configForm" ref="configForm" :rules="configFormRules" size="small" label-width="160px">
          <el-form-item label="每天上架开始时间：" prop="shelfTime">
            <el-time-picker v-model="configForm.shelfTime" placeholder="选择时间" value-format="HH:mm:ss">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="每天上架结束时间：" prop="offShelfTime">
            <el-time-picker v-model="configForm.offShelfTime" placeholder="选择时间" value-format="HH:mm:ss">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="UV总量最大值：" prop="maxTotalUV">
            <el-input v-model="configForm.maxTotalUV" class="a_input"></el-input>
            <div class="tips">UV总量最大值达标后产品自动下架</div>
          </el-form-item>
        </el-form>
      </div>
      <div class="" slot="footer">
        <el-button size="small" type="success" plain @click="saveInfo()">确定</el-button>
        <el-button size="small" type="default" plain @click="closeConfig">取消</el-button>
      </div>
    </glsx-dialog>
  </div>
</template>

<script>
import {
  GETPRODUCTLIST,
  PRODUCTEXPORTURL,
  PRODUCTSETTING,
  GETPRODUCTSETTING,
  PRODUCTUPLOW,
} from "@/http/interface";
import { showToast, showModal } from "@/common/util";
import { exportExcel } from "@/common/common";
import GlsxDialog from "@/components/Dialog/index";
export default {
  components: {
    GlsxDialog,
  },
  data() {
    return {
      formInline: {},
      configForm: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 30, 40],
      pageSize: 10,
      cityList: [],
      ORDERTYPELIST: {
        80: "会员",
        90: "商品",
        100: "话费",
      },
      typeList: [
        { name: "会员", id: 80 },
        { name: "商品", id: 90 },
        { name: "话费", id: 100 },
      ],
      eloading: false,
      orderTime: [],
      showDialog: false,
      createDate: [],
      configFormRules: {
        shelfTime: [
          {
            required: true,
            message: "请选择每天开始上架时间",
            trigger: "change",
          },
        ],
        offShelfTime: [
          {
            required: true,
            message: "请选择每天结束上架时间",
            trigger: "change",
          },
        ],
        maxTotalUV: [
          { required: true, message: "请输入UV总量最大值", trigger: "blur" },
        ],
      },
      productId: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.orderTime = [];
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));
      let n = this.orderTime;

      if (n && n.length > 0) {
        oParams.sDate = n[0];
        oParams.eDate = n[1];
      }

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GETPRODUCTLIST(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 导出excel
    exportExcel() {
      let _this = this;
      let oParams = this.getParams();
      let options = {};
      options.method = "GET";
      options.data = oParams;
      options.url = PRODUCTEXPORTURL;
      options.fileName = "产品列表.xlsx";
      options.success = function () {
        _this.eloading = false;
      };
      _this.eloading = true;
      exportExcel(options);
    },

    // 新增产品
    addEvent() {
      this.$router.push({ name: "addProduct" });
    },

    // 打开产品配置
    openConfig(row) {
      this.showDialog = true;
      this.productId = row.id;
      GETPRODUCTSETTING({ productId: row.id }).then((res) => {
        this.configForm = res.data || {};
      });
    },

    // 关闭产品配置
    closeConfig() {
      this.showDialog = false;
      this.productId = "";
      this.configForm = {};
      this.$refs.configForm.clearValidate();
    },

    // 保存配置
    saveInfo() {
      let _this = this;
      console.log(this.configForm);
      _this.$refs.configForm.validate((valid) => {
        if (!valid) {
          return false;
        }

        let oParams = JSON.parse(JSON.stringify(_this.configForm));
        oParams.productId = _this.productId;
        PRODUCTSETTING(oParams).then((res) => {
          if (res.code == 200) {
            showToast({
              message: "配置成功",
              type: "success",
            });
            _this.closeConfig();
            //_this.getList();
          }
        });
      });
    },

    // 产品上下架
    update(row) {
      let _this = this;
      let msg = row.enableStatus == 1 ? "下架" : "上架";
      showModal({
        message: "确定" + msg + "该产品吗？",
        showCancelButton: true,
        success: () => {
          PRODUCTUPLOW({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: msg + "成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 编辑产品
    edit(row) {
      this.$router.push({ name: "editProduct", query: { id: row.id } });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  background: #fff;
}
.action_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
